package com.zhangjianling.premp.utils;

import com.zhangjianling.premp.entity.sys.SysUser;
import com.zhangjianling.premp.realm.MyShiroRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @author jackLiang
 * @version V0.0.1
 * @date 2017年12月13日10:54:46
 */
public final class ShiroUtils {

    public static Subject getSubjct() {
        return SecurityUtils.getSubject();
    }

    public static SysUser getUser() {
        return (SysUser) getSubjct().getPrincipal();
    }

    public static String getUserId() {
        return getUser().getUserId();
    }

    public static void logout() {
        getSubjct().logout();
    }

    /**
     * 僅僅衹是測試方法，先留著以後說。
     */
    public static void clearCache() {
        Subject subject=SecurityUtils.getSubject();
        RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        MyShiroRealm userRealm = (MyShiroRealm)securityManager.getRealms().iterator().next();
        userRealm.clearCachedAuthorizationInfo(subject.getPrincipals());
    }

}